package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalUserCardService;
import com.qdlc.p2p.biz.service.activity.ActivityPlanEnum;
import com.qdlc.p2p.biz.service.activity.ActivityPlanException;
import com.qdlc.p2p.biz.service.activity.ActivityPlanHandlers;
import com.qdlc.p2p.biz.service.activity.ActivityPlanParam;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeEnum;
import com.qdlc.p2p.biz.service.activity.param.ActivityProgrammeUserCardParam;
import com.qdlc.p2p.common.constant.UserCardStatus;
import com.qdlc.p2p.common.enums.EnumCardType;
import com.qdlc.p2p.common.enums.EnumUserCardStatus;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.MailUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AdditionalUserCardDao;
import com.qdlc.p2p.dal.dto.AdditionalUserCard;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AdditionalUserCardModel;

/**
 * 功能描述: 用户卡片
 * @author linya
 * @date 2016-5-6
 */
@Service
public class AdditionalUserCardServiceImpl implements AdditionalUserCardService {

	@Resource
	AdditionalUserCardDao additionalUserCardDao;
	
	/**
	 * 给用户送卡片
	 * @param user
	 * @param cardType
	 * @throws ActivityPlanException 
	 */
	public void giveCard(User user, int cardType){
		//定义活动计划
    	try {
			ActivityPlanParam planParam = new ActivityPlanParam();
			planParam.setActivityPlan(ActivityPlanEnum.USER_CARD);
			planParam.setUser(user);
			ActivityProgrammeUserCardParam programmeParam = new ActivityProgrammeUserCardParam();
			programmeParam.setCardType(EnumCardType.getEnumCardType(cardType));
			planParam.addProgrammeParam(ActivityProgrammeEnum.USER_CARD, programmeParam);
			ActivityPlanHandlers.customHandle(planParam);
		} catch (ActivityPlanException e) {
			e.printStackTrace();
			MailUtil.sendMail("给用户" + user.getUserName() + "发送" + EnumCardType.getEnumCardType(cardType).getName() + "失败,需要补发.");
		}
	}
	
	@Override
	public void save(AdditionalUserCard userCard) {
		additionalUserCardDao.save(userCard);
	}

	@Override
	public int updateUserCardInvalid(long id) {
		return additionalUserCardDao.update(UserCardStatus.STATUS_INVALID, UserCardStatus.STATUS_NO_USE, id);
	}
	
	@Override
	public int useUserCard(long userId, long[] cardIds) {
		int count = additionalUserCardDao.useUserCard(userId, cardIds);
		return count;
	}

	@Override
	public List<AdditionalUserCard> findUserCards(long userId, long[] cardIds){
		List<AdditionalUserCard> list = additionalUserCardDao.findUserCards(userId, cardIds);
		return list;
	}
	
	@Override
	public List<AdditionalUserCardModel> findUsableUserCardList() {
		List<AdditionalUserCardModel> modelList = null;
		List<AdditionalUserCardModel> cardRuleList = additionalUserCardDao.findUsableUserCardList();
		if (CollectionUtils.isNotNullOrEmpty(cardRuleList)) {
			modelList = new ArrayList<AdditionalUserCardModel>();
			for (AdditionalUserCardModel item : cardRuleList) {
				modelList.add(dealUserCardModel(item));
			}
		}
		return modelList;
	}

	@Override
	public PageDataList<AdditionalUserCardModel> list(AdditionalUserCardModel model) {
		PageDataList<AdditionalUserCardModel> pageDataList = new PageDataList<AdditionalUserCardModel>();
		PageDataList<AdditionalUserCardModel> dataList = additionalUserCardDao.list(model);
		List<AdditionalUserCardModel> list = dataList.getList();
		List<AdditionalUserCardModel> modelList = new ArrayList<AdditionalUserCardModel>();
		if (CollectionUtils.isNotNullOrEmpty(list)) {
			for (AdditionalUserCardModel item : list) {
				modelList.add(dealUserCardModel(item));
			}
		}
		pageDataList.setPage(dataList.getPage());
		pageDataList.setList(modelList);
		return pageDataList;
	}

	/**
	 * 处理用户卡片信息（主要用于显示）
	 *
	 * @param model
	 * @return
	 * @author Kaiju Zhao
	 */
	private AdditionalUserCardModel dealUserCardModel(AdditionalUserCardModel model) {
		model.setTypeStr(EnumCardType.getName(model.getType()));
		int status = model.getStatus();
		String statusStr = EnumUserCardStatus.getName(status);
		Date expiredTime = model.getExpiredTime();
		if (null != expiredTime) {
			int remainDays = DateUtil.daysBetween(DateUtil.getNow(), model.getExpiredTime());
			model.setRemainDays(remainDays);
			if (remainDays < 0
					&& UserCardStatus.STATUS_NO_USE == status) {
				statusStr = EnumUserCardStatus.STATUS_EXPIRED.getName();
			}
		}
		model.setStatusStr(statusStr);
		return model;
	}

}
